package jcataloger.beans;

import java.util.Locale;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

/**
 * This class creates an index document which consists of searchable fields. 
 * @author sekobey
 *
 */
public class FileDocument {
	
	public enum FIELDS {diskName, fileName, fileNameLower, path, type};
	
	/**
	 * creates a document.
	 * @param diskName shows to which disk the document belongs.
	 * @see ImageItem
	 * @param item contains the indexable data
	 * @return an indexable document
	 * @throws java.io.FileNotFoundException
	 */
	public static Document document(String diskName, ImageItem item) 
		throws java.io.FileNotFoundException {
		
		// make a new, empty document
	    Document doc = new Document();
	    
	    // Add the name of the disk to help removing disk images.
	    doc.add(new Field("diskName", diskName, Field.Store.YES, Field.Index.UN_TOKENIZED));
	    
	    // Add the name of the file
	    doc.add(new Field("fileName", item.getName(), Field.Store.YES, Field.Index.UN_TOKENIZED));
	    
	    // TODO match case olmadiginda bu alan aramalarda kullanilir. Fakat locale gore kucuk harfe donusturulmeli.Ornek INSTALL
	    // Add the lowercase fileName
	    doc.add(new Field("fileNameLower", item.getName().toLowerCase(new Locale("ISO-8859-9")), Field.Store.YES, Field.Index.UN_TOKENIZED));
	    
	    // Add the path of the file as a field named "path".  Use a field that is 
	    // indexed (i.e. searchable), but don't tokenize the field into words.
	    doc.add(new Field("path", item.getPath(), Field.Store.YES, Field.Index.UN_TOKENIZED));
	    
	    // Add if the file is directory or file
	    doc.add(new Field("type", item.isFile() ? "file" : "directory", Field.Store.YES, Field.Index.UN_TOKENIZED));

	    // Add the length of the file
	    doc.add(new Field("length", item.getLength(), Field.Store.YES, Field.Index.UN_TOKENIZED));
	    
	    // Add the type of the file
	    doc.add(new Field("extension", item.getExtension(), Field.Store.YES, Field.Index.UN_TOKENIZED));
	    
	    // return the document
	    return doc;
	}
}
